<?php
/**
 * 后台用户管理
 *
 * @category     Shuxun Cms
 * @date         2015年11月4日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         AdminuserController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
class AdminuserController extends  ControllerBase
{

	/**
	 * 列表
	 */
	public  function  indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);

		$act  = $this->request->get('act');
		if($act == 'remove'){
			$this->_remove();
		}

		$dataList = $this->_getDataList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){

			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('adminuser','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);

		}else{
			$this->view->setVar('full_page',1);
		}
	}

	/**
	 * 编辑
	 */
	public  function editAction()
	{
		$user_id  =  $this->request->get('id','int',0);
		$adminUserModel = new SModel('admin_user');
		$adminUser = $adminUserModel->getObj('user_id='.$user_id);
		unset($adminUser->password);
		$this->view->setVar('item',$adminUser);
		$this->view->pick('adminuser/edit');
	}

	/**
	 * 修改密码
	 */
	public  function  changepwdAction()
	{
		if($this->request->isPost()){
			$userId = $this->getCore()->auth->getIdentity();
			$password = $this->request->get('password');
			$password = stripcslashes($password);
			$adminUserModel = new SModel('admin_user');
			$adminUserModel->setData(array(
				'password' => md5($password)
			));
			$adminUserModel->update('user_id='.$userId);
			$this->redirect('adminuser/changepwd');
		}
	}

	/**
	 * 个人信息
	 */
	public  function  profileAction()
	{
		$user_id  =  $this->getCore()->auth->getIdentity();
		$adminUserModel = new SModel('admin_user');
		$adminUser = $adminUserModel->getObj('user_id='.$user_id);
		unset($adminUser->password);
		$this->view->setVar('item',$adminUser);
		$this->view->pick('adminuser/edit');
	}

	/**
	 * 删除
	 */
	public  function addAction()
	{
		$this->view->pick('adminuser/edit');
	}

	/**
	 * 保存
	 */
	public function  saveAction()
	{
		$user_id  =  $this->request->get('user_id','int',0);
		$postData =  $_POST;
		$adminUserModel = new SModel('admin_user');

		if(empty($postData['password'])){
			unset($postData['password']);
		}else{
			$postData['password'] = md5(trim($postData['password']));
		}


		if($user_id){
			$postData['modified'] = time();
			$adminUserModel->setData($postData);
			$adminUserModel->update('user_id='.$user_id);
		}else{
			$postData['created'] = time();
			$adminUserModel->setData($postData);
			$adminUserModel->add();
		}
		return $this->response->redirect('adminuser');
	}

	/**
	 * 验证
	 */
	public function validAction()
	{
		$id = $this->request->get('id','int',0);
		$name = $this->request->get('name');
		$param = $this->request->get('param');
		$result = array('status'=>'n','info'=>'');

		$customerDB = new SModel('admin_user');
		$where = sprintf("user_id!=%s and %s='%s'",intval($id),$name,trim($param));
		$customer = $customerDB->getObj($where);
		if($customer){
			$result['status'] = 'n';
			$result['info']   = '该值不能重复!';
		}else{
			$result['status'] = 'y';
		}
		$this->sendJson($result);
	}

	/**
	 * 删除数据
	 */
	protected  function _remove()
	{
		$user_id  =  $this->request->get('id','int',0);
		$adminUserModel = new SModel('admin_user');
		$adminUserModel->del('user_id='.intval($user_id));
	}

	/**
	 * 加载数据
	 */
	protected   function  _getDataList()
	{

		$filter = array();

		$page = $this->request->get('page','int',1);

		$where = '1=1';

		if(!empty($_REQUEST['username'])){
			$where = 'username like "%'.addslashes($_REQUEST['username']).'%"';
			$filter['username'] = $_REQUEST['username'];
		}

		if(!empty($_REQUEST['is_active'])){
			$where = 'is_active ='.intval($_REQUEST['is_active']);
			$filter['is_active'] = $_REQUEST['is_active'];
		}

		$userQuery = new SQuery('admin_user as u');
		$userQuery->fields = 'u.*';
		$userQuery->page =  $page;
		$userQuery->where = $where;
		$userQuery->order = 'u.user_id desc';
		$userQuery->pagesize = 12;

		$items = $userQuery->find();

    	$dataList = new \stdClass();
    	$dataList->totalPage = $userQuery->paging->getTotalPage();
    	$dataList->currentPage = $userQuery->paging->getIndex();
    	$dataList->items = $items;
    	$dataList->totalSize = $userQuery->paging->getTotalSize();
    	$dataList->filter = $filter;

		return  $dataList;

	}
}